package com.dcbeida.activity.addnewpatient;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.bean.AddPatient;
import com.dcbeida.bean.HistoryOfSmokingBean;
import com.dcbeida.bean.NewPatient;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class AddNewPatientPresenter implements AddNewPatientContract.Present {


    private AddNewPatientContract.View view;

    private ModelUtils model;

    private Activity act;

    public AddNewPatientPresenter(AddNewPatientContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        model = ModelUtils.newInstance();
        this.act = act;

    }


    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void getData(NewPatient patient) {
        Gson gson = new Gson();
        String str = "{\"uid\":"+MyApplication.user.getUid() + ",\"shopid\":"+MyApplication.user.getShopid() + ",\"patient\":" + gson.toJson(patient) + "}";
//      model.getDate("addpatient", gson.toJson(patient), new AsynCallBack(act, HistoryOfSmokingBean.class, lis));
        model.getDate("addpatient", str, new AsynCallBack(act, AddPatient.class, lis));


    }


    final class AsynCallBack<T> extends DialogCallback<T> {

        public AsynCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                    view.getDataSuccess((AddPatient) data);
            }

        }


        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip(act.getString(R.string.wlyc));

        }
    }


}
